package com.naiterui.ehp.bs.patient.controller;

import com.naiterui.common.repository.page.PageParamVO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.domain.Patient;
import com.naiterui.ehp.bp.support.context.ResourceHolder;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.security.SettingConfig;
import com.naiterui.ehp.bp.support.utils.JsonMapper;
import com.naiterui.ehp.bp.utils.lang.StringUtil;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.IPatientAccountService;
import com.naiterui.ehp.bs.patient.service.IPatientCaseFileService;
import com.naiterui.ehp.bs.patient.service.IPatientService;
import com.naiterui.ehp.bs.patient.utils.constants.PatientConstant;
import com.naiterui.ehp.bs.patient.utils.constants.StringConstant;
import com.naiterui.ehp.bs.patient.vo.account.MissionVO;
import com.naiterui.ehp.bs.patient.vo.account.PatientAccountDetailVO;
import com.naiterui.ehp.bs.patient.vo.account.PersonalCenterVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * 患者账户业务控制器
 *
 * @author Amber
 * @since 1.0.0
 */
@Api(value = "患者商户相关", tags = SwaggerTag.PATIENT)
@RequestMapping("ap/patient")
// @Controller
public class ApPatientAccountController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApPatientAccountController.class);

    @Autowired
    IPatientService patientService;
    @Autowired
    IPatientCaseFileService patientCaseFileService;
    @Autowired
    IPatientAccountService patientAccountService;

    /**
     * 获取诊金任务列表
     * PatientAccountController.getMissions()
     *
     * @param patientId
     *
     * @return
     *
     * @Author Amber
     * @Date 2016年1月14日
     * @since 1.0.0
     */
    @ResponseBody
    @RequestMapping("missions")
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public MissionVO getMissions(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        // 参数校验
        if (patientId == null) {
            LOGGER.error("get missions params error。 patientId is null");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        MissionVO missionVO = new MissionVO();
        Patient patient = this.patientService.getPatient(patientId);

        if (patient == null) {
            LOGGER.error("get missions error。 patient not exist 。patientId {}", patientId);
            throw new BusinessException(ExceptionCodes.PATIENT_NOT_EXIST);
        }

        if (patient.getAccountStatus() != 0) {
            LOGGER.error("get missions error。 patient account status srong 。patientId {}", patientId);
            throw new BusinessException(ExceptionCodes.PATIENT_ACCOUNT_ERROR);
        }

        // 完善个人资料文案
        String completePersonal = "";
        if (patient.getPhone() == null) {
            completePersonal = StringUtil.templateFormat(StringConstant.COMPLETE_PERSONAL_GIFT,
                                                         PatientConstant.GIFT_COMPLETE_PERSONAL / PatientConstant.ACCOUNT_UNIT + ".00");
        }
        missionVO.setCompletePersonal(completePersonal);

        // 是否上传过病历档案
        Boolean haveCaseFile = this.patientCaseFileService.haveCaseFile(patientId);
        String completeCaseFile = "";
        if (!haveCaseFile) {
            completeCaseFile = StringUtil.templateFormat(StringConstant.COMPLETE_CASEFILE_GIFT,
                                                         PatientConstant.GIFT_COMPLETE_CASEFILE / PatientConstant.ACCOUNT_UNIT + ".00");
        }
        missionVO.setCompleteCaseFile(completeCaseFile);

        // 下单
        missionVO.setPlaceOrder(StringUtil.templateFormat(StringConstant.PLACE_ORDER_GIFT, PatientConstant.PLACE_ORDER_GIFT));

        LOGGER.info("get missions params SUCCESS。 patientId {}", patientId);
        return missionVO;
    }

    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping("personal/center")
    public PersonalCenterVO getMyAccount(Long patientId) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        // 参数校验
        if (patientId == null) {
            LOGGER.error("patient personal center params ERROR patientId is null");
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        // 患者实体
        Patient patient = this.patientService.getPatient(patientId);

        // 用户信息校验
        if (patient == null) {
            LOGGER.error("patient personal center patient not exist patientId {}", patientId);
            throw new BusinessException(ExceptionCodes.PATIENT_NOT_EXIST);
        }

        if (patient.getAccountStatus() != 0) {
            LOGGER.error("patient personal center ERROR; patient account status wrong。 patientId {}", patientId);
            throw new BusinessException(ExceptionCodes.PATIENT_ACCOUNT_ERROR);
        }

        // 账户信息
        PersonalCenterVO accountVO = this.patientAccountService.getPersonalCenterInfo(patient);

        accountVO.setPatientId(patientId);
        accountVO.setPhoto(patient.getHeadUrl());
        LOGGER.info("patient personal center SUCCESS patientId {}", patientId);
        return accountVO;
    }

    @ResponseBody
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @RequestMapping("account")
    public PatientAccountDetailVO accountDetail(Long patientId, PageParamVO pageParam) throws BusinessException {
        if (null == patientId) {
            patientId = (Long) ResourceHolder.getResource(SettingConfig.PATIENT_ID);
        }
        // 参数校验
        if (patientId == null) {
            LOGGER.error("---account detail params ERROR patientId is null, page {}, num {}", pageParam.getPage(),
                         pageParam.getNum());
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        PatientAccountDetailVO accountDetail = this.patientAccountService.getAccountDetail(patientId, pageParam.getPage(), pageParam.getNum());
        LOGGER.info("---account detail params SUCCESS patientId {}, page {}, num {}", patientId, pageParam.getPage(),
                    pageParam.getNum());
        return accountDetail;
    }

    /**
     * 推荐订单完成赠送诊金
     */
    @ResponseBody
    @RequestMapping(value = "order/income", method = RequestMethod.POST)
    @ApiOperation(value = "", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public void orderImcome(String orderInfo) throws BusinessException {
        // 参数校验
        if (StringUtils.isEmpty(orderInfo)) {
            LOGGER.error("---order income params error. orderInfo {}", orderInfo);
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }

        Map<String, String> params = JsonMapper.toObject(orderInfo, new ParameterizedTypeReference<Map<String, String>>() {
        });

    }
}
